home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <sys/param.h>
- #include <dirent.h>
- #include "crtlocal.h"
-
- int getdirentries (int fd, char *buffer, int size, long *actualsize)
- {
- struct dirent *buf = (struct dirent *)buffer;
- int idx = crt_fd_tab[fd].flags&~O_CATALOG;
- OSErr err;
- CInfoPBRec cPB;
- while (size >= sizeof(struct dirent))
- {
- /* get information about dir */
- cPB.hFileInfo.ioCompletion = (ProcPtr)0L;
- cPB.hFileInfo.ioNamePtr = (StringPtr)&(buf->d_name[0]);
- cPB.hFileInfo.ioVRefNum = crt_ioVRefNum;
- cPB.hFileInfo.ioFDirIndex = ++idx;
- cPB.hFileInfo.ioDirID = crt_fd_tab[fd].fd;
-
- memset(buf, 0, sizeof(struct dirent));
- err = errtran(PBGetCatInfoSync(&cPB));
- if (err)
- {
- size = 0;
- if (err != -43) return -1;
- }
- else
- {
- buf->d_fileno = cPB.hFileInfo.ioDirID; /* file number of entry */
- buf->d_namlen = *buf->d_name; /* length of string in d_name */
- BlockMove(buf->d_name+1, buf->d_name, buf->d_namlen);
- buf->d_name[buf->d_namlen] = 0;
- buf->d_reclen = buf->d_name-(char *)buf+(buf->d_namlen+1); /* length of this record */
- buf->d_reclen = ((buf->d_reclen-1)|3)+1;
- size -= buf->d_reclen;
- buf = (struct dirent *)(buf->d_reclen+(char *)buf);
- }
- }
- *actualsize = idx;
- crt_fd_tab[fd].flags = O_CATALOG|idx;
- return ((char *)buf-buffer);
- }
-
- int read(int fd, void *buf, unsigned size)
- {
- if ((crt_fd_tab[fd].fd&-256)==100<<8) /* console */
- {
- return read_console_stream(crt_fd_tab[fd].fd&255, buf, size);
- }
- else if (crt_fd_tab[fd].flags & O_PIPE)
- {
- return readpipe(fd, buf, size);
- }
- else if (crt_fd_tab[fd].flags & O_CATALOG)
- {
- long actual;
- int cnt = getdirentries(fd, buf, size, &actual);
- if (cnt < 0) return cnt;
- return actual;
- }
- else
- {
- int i;
- OSErr err;
- IOParam pb;
- pb.ioCompletion = 0;
- pb.ioRefNum = crt_fd_tab[fd].fd;
- pb.ioBuffer = buf;
- pb.ioReqCount = size;
- pb.ioPosMode = fsAtMark;
- PBReadSync((ParmBlkPtr)&pb);
- err = errtran(pb.ioResult);
- return pb.ioActCount;
- }
- }
-